package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.product.ProductAttrValueDao;
import cn.fastoo.common.dao.product.ProductCategoryAttrgroupAttrDao;
import cn.fastoo.common.dao.product.ProductCategoryAttrgroupAttrValueDao;
import cn.fastoo.common.entity.product.ProductAttrValue;
import cn.fastoo.common.entity.product.ProductCategoryAttrgroupAttr;
import cn.fastoo.common.entity.product.ProductCategoryAttrgroupAttrValue;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product/attr/value")
public class ProductAttrValueController {


    @Autowired
    private ProductAttrValueDao dao;
    @Autowired
    private ProductCategoryAttrgroupAttrDao productCategoryAttrgroupAttrDao;
    @Autowired
    private ProductCategoryAttrgroupAttrValueDao productCategoryAttrgroupAttrValueDao;


    @RequestMapping
    public String index() {
        return "product/attr/value/index";
    }


    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute ProductAttrValue newData, QuerySearch querySearch, String op,Long productCategoryAttrgroupAttrId) {
        Search search = querySearch.getSearch();
        if(productCategoryAttrgroupAttrId!=null){
            ProductCategoryAttrgroupAttr pcaa = productCategoryAttrgroupAttrDao.findById(productCategoryAttrgroupAttrId);
            if(pcaa!=null){
                search.addSearchFilter("EQ_productAttrId",pcaa.getProductAttrId());
            }
            List<String> notInIds = productCategoryAttrgroupAttrValueDao.findProductAttrValueIdByProductCategoryAttrgroupAttrId(productCategoryAttrgroupAttrId);
            if(Collections3.isNotEmpty(notInIds)){
                search.addSearchFilter("NOTIN_id",notInIds);
            }
        }

        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                ProductAttrValue oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.find(search),querySearch);
    }



}
